package com.zhijiang.common.util;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

public class Util {
    public static String get(String url) {

        try {
            URL urlobj=new URL(url);
            URLConnection connection=urlobj.openConnection();
            InputStream is=connection.getInputStream();
            byte[] b=new byte[1024];
            int len;
            StringBuilder sb=new StringBuilder();
            while((len=is.read(b))!=-1) {
                sb.append(new String(b,0,len,"UTF-8"));
            }
            return sb.toString();
        } catch (Exception e) {

            e.printStackTrace();
        }
        return null;
    }

    public static String post(String url, String data) {
        try {
            URL urlobj=new URL(url);
            URLConnection connection=urlobj.openConnection();
            //要发送数据必须设置为true
            connection.setDoOutput(true);
            //获取输出流
            OutputStream os=connection.getOutputStream();
            os.write(data.getBytes("UTF-8"));
            os.close();
            //获取输入流得到返回的数据
            InputStream is=connection.getInputStream();
            byte[] b=new byte[1024];
            int len;
            StringBuilder sb=new StringBuilder();
            while((len=is.read(b))!=-1) {
                sb.append(new String(b,0,len));
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }


}
